function reset(){
	$("#frm_login")[0].reset();
}
function r_reset(){
	$("#frm_register")[0].reset();
}
function register(){
	var uname=$("#r_uname").val();
	if(uname.length==0){
		layer.msg("用户名不能为空！",{
			icon:2,
			time:1000
		});
		return;
	}
	
	var upwd=$("#r_upwd").val();
	var patrn=/^[A-Za-z0-9._]{8,16}$/
	if(!patrn.exec(upwd)){
		layer.msg("请输入8~16位密码！",{
		    icon:5,
		    time:1000
			
		});
			return;
	}
	
	var cupwd=$("#r_cupwd").val();
	var patrn=/^[A-Za-z0-9._]{8,16}$/
	if(!patrn.exec(cupwd)){
		layer.msg("请再次输入8~16密码！",{
		    icon:5,
		    time:1000
			
		});
			return;
	}
	if(upwd!=cupwd){
		layer.tips("两次密码不一致！","#r_cupwd",{
			tips:[2,"#ff0000"]
		});
		    return;
	}
	
	var email=$("#r_email").val();
	if(email.length==0){
		layer.msg("请输入邮箱！",{
			icon:8,
			time:1000
		});
		return;
	}
	
	var email=$("#r_email").val();
	var reg=/^[A-Za-z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
	if(!reg.test(email)){
		layer.tips("邮箱输入有误！","#r_email",{
			tips:[2]
		});
		    return;
	}
	//ajax根据json的结果，跳到登录url   json
	jQuery.ajax({
		url:"dataJSON/register.json",
		data:{},
		type:"get",
		dataType:"json",
		success:function(result){
			var temp=result.code;
			/*layer.alert(temp);*/
			
			if(temp==1){
				window.location.href="login.html";
			}else{
				layer.msg("注册失败！",{icon:2});
			}
		},
		error:function(){
			layer.msg("访问服务器失败！",{icon:2});
		}
	});
	
	
	
	/*window.location.href="login.html";*/

}


jQuery(function($) {

	var code = drawPic();
	document.getElementById("changeImg").onclick = function(e) {
		e.preventDefault();
		code = drawPic();
	}

	$("#login").click(function(event) {
		var username = $('#usernameId').val();
		var password = $('#passwordId').val();
		var verity = $('#verityId').val();
		//	var code = $('#login').data('code')
		//	console.log(username,password,verity,code);
       
		var username = $('#usernameId').val();
		if(username.length == 0) {
			layer.tips("用户名不能为空!", "#usernameId", {
        	tips: [2, "#FF0000"],
            time: 1000,
			});
				return;
		}
		var password = $('#passwordId').val()
		if(password.length == 0) {
			layer.tips("密码不能为空!", "#passwordId", {
				tips: [2, "#FF0000"],
        		time: 1000,
            });
		        return;
        }
		
		
		var patrn=/^(\w){8,16}$/
		if(!patrn.exec(password)){
			layer.tips("密码应在8~16位之间！","#passwordId",{
				tips:[2, "#FF0000"],
        		time: 1000,
			});
			return false;
		}
		
		
		if(verity.length == 0) {
			layer.tips("验证码不能为空!", "#verityId", {
				tips: [2, "#FF0000"],
				time: 1000,
			});
		        return;
		}
		

		var patrn = /^[A-Za-z0-9]{4}$/
		if(!patrn.exec(verity)) {
			layer.tips("验证码有误!", "#verityId", {
				tips: 2,
				time: 1000
			});
			return;
		}
		if(verity.toLowerCase() != code.toLowerCase()) {
			layer.tips("验证码输入有误!", "#verityId", {
				tips: 1,
				time: 1000
			});
			return;
		}
		$.ajax({
			url: "dataJSON/login.json",
			type: 'get',
			dataType: "json",
			success: function(result) {
				
				
				
				
			var userinfo=	result.data;
				var temp=false;
				
				/*layer.alert(JSON.stringify(userinfo),{icon:1});*/
				
				
				$.each(userinfo, function(login,value) {
					if(username==value.username&&password==value.password){
						temp=true;
						return;
					}
					});
					
				if(temp==false)
					layer.msg("登陆失败！", {
						icon: 2,
						time: 1000
					});
				
			/*(console.log)*//*layer.alert    (JSON.stringify(value,username),{icon:1});
				});
				return;
				
				
				
				if(userinfo.username==username)&&userinfo.password==password)*/
				
				
				
				else{
					var state=$("#rememberPwd").prop("checked");
					if(state==true){
						localStorage.setItem("username",username);
						localStorage.setItem("password",password);
					}else{
						localStorage.clear();
					}
					
					sessionStorage.setItem("username",username);
					location.href = "manager.html";
					
				} 
				/*else {
					layer.alert("登陆失败！", {
						icon: 2,
						time: 1000
					});
				}*/
			},
			error: function() {
				layer.msg("请求服务器失败！", {
					icon: 2,
					time: 1000
				});

			}
		});
	});

});

/**生成一个随机数**/
function randomNum(min, max) {
	return Math.floor(Math.random() * (max - min) + min);
}
/**生成一个随机色**/
function randomColor(min, max) {
	var r = randomNum(min, max);
	var g = randomNum(min, max);
	var b = randomNum(min, max);
	return "rgb(" + r + "," + g + "," + b + ")";
}

/**绘制验证码图片**/
function drawPic() {
	var canvas = document.getElementById("canvas");
	var width = canvas.width;
	var height = canvas.height;
	//获取该canvas的2D绘图环境 
	var ctx = canvas.getContext('2d');
	ctx.textBaseline = 'bottom';
	/**绘制背景色**/
	ctx.fillStyle = randomColor(180, 240);
	//颜色若太深可能导致看不清
	ctx.fillRect(0, 0, width, height);
	/**绘制文字**/
	var str = 'ABCEFGHJKLMNPQRSTWXY123456789';　　　　
	var code = "";　　　　　 //生成四个验证码
	for(var i = 1; i <= 4; i++) {
		var txt = str[randomNum(0, str.length)];
		code = code + txt;
		ctx.fillStyle = randomColor(50, 160);
		//随机生成字体颜色
		ctx.font = randomNum(15, 40) + 'px SimHei';
		//随机生成字体大小
		var x = 10 + i * 25;
		var y = randomNum(25, 35);
		var deg = randomNum(-45, 45);
		//修改坐标原点和旋转角度
		ctx.translate(x, y);
		ctx.rotate(deg * Math.PI / 180);
		ctx.fillText(txt, 0, 0);
		//恢复坐标原点和旋转角度
		ctx.rotate(-deg * Math.PI / 180);
		ctx.translate(-x, -y);
	}

	/**绘制干扰线**/
	for(var i = 0; i < 3; i++) {
		ctx.strokeStyle = randomColor(40, 180);
		ctx.beginPath();
		ctx.moveTo(randomNum(0, width / 2), randomNum(0, height / 2));
		ctx.lineTo(randomNum(0, width / 2), randomNum(0, height));
		ctx.stroke();
	}
	/**绘制干扰点**/
	for(var i = 0; i < 50; i++) {
		ctx.fillStyle = randomColor(255);
		ctx.beginPath();
		ctx.arc(randomNum(0, width), randomNum(0, height), 1, 0, 2 * Math.PI);
		ctx.fill();
	}
	$("#login").data('code', code);
	return code;
}

/**/